﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;
using VIW = CPPEI.ETL.Domain.Views;

namespace CPPEI.ETL.WebApi.DtSer.ExtDS
{
    [CPPEI.WebApi.AuthGetAction("ExtDS", "GetDataService", "1.0")]
    public class GetDataServiceAction : BaseAction<GetDataServiceParams, GetDataServiceResult>
    {
        protected override ActionResult<GetDataServiceResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            GetDataServiceParams opParams)
        {
            SER.DtSerService dtSerService = new SER.DtSerService();
            ServiceResult<VIW.VDtSer> serResult = dtSerService.GetDtSer(opParams.ServiceId, CPPEI.ETL.Domain.DEF.DataServiceTypeId.ExtDS);
            if (serResult.IsSuccess)
            {
                GetDataServiceResult result = new GetDataServiceResult();
                result.Id = serResult.Data.Id;
                result.Name = serResult.Data.Name;
                string[] addressArr = serResult.Data.Address.Split(new char[] { ';' });
                result.Assembly = addressArr[0];
                result.Type = addressArr[1];
                result.Description = serResult.Data.Description;
                result.IsEnabled = serResult.Data.IsEnabled;
                return new ActionResult<GetDataServiceResult>(result);
            }
            else
            {
                return new ActionResult<GetDataServiceResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class GetDataServiceParams
    {
        [Parameter]
        public Guid ServiceId { get; set; }
    }

    public class GetDataServiceResult
    {
        public Guid Id { get; set; }

        public string Name { get; set; }

        /// <summary>
        /// 外部扩展服务程序集
        /// </summary>
        public string Assembly { get; set; }

        /// <summary>
        /// 外部扩展服务类型
        /// </summary>
        public string Type { get; set; }

        public string Description { get; set; }

        public bool IsEnabled { get; set; }
    }
}
